﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;
using System.Web;

namespace YouHP.Pages.User
{
    public class LogAdd : YouHP.Pages.User.UserBasePage
    {
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }


        protected void HandleCommon()
        {
            this.SetTitle("写日志");
            this.AddLocation("写日志", "/User/LogAdd.aspx");
        }

        protected void HandlePost()
        {
            LogTopicInfo topic = ValidateForm();
            if (ErrorCount > 0) return;
            bool succ = LogTopicBLL.Instance.AddLogTopic(topic);
            LogTopicInfo TopicData = topic;
            string[] tagArr = null;
            if (succ)
            {
                //添加动态
                UserActionInfo Info = new UserActionInfo();
                Info.UserId = UserData.Id;
                Info.UserName = UserData.UserName;
                Info.ActionTime = DateTime.Now;
                Info.ActionType = Constants.ACTION_LOG;
                Info.ActionContent = "&nbsp;<a href=\"" + this.GetUrl("/Profile/Log.aspx?uid=" + UserData.Id + "&lid=" + TopicData.Id) + "\"target=\"_blank\">" + TopicData.Title + "</a>&nbsp;";
                UserActionBLL.Instance.AddUserAction(Info);
                tagArr = Regex.Split(TopicData.Tags, "\\s+");
                UserBLL.Instance.UpdateUserCreditsByPost(Info.UserId);//设置用户积分
                if (tagArr.Length > 0)
                {
                    if (tagArr[0] != "")
                    {
                        TagBLL.Instance.CreateTopicTag(tagArr, 2, TopicData.Id, Info.UserId);
                    }
                }

                this.Redirect("/User/LogList.aspx");
            }
            else
            {
                this.AddErrLine("发表日志失败");
            }
        }


        protected void HandleGet()
        {
            //this.AddScriptSrc(TinyMCE.Path);
            //this.AddScript(TinyMCE.InitAdvance("logbody"));

        }

        LogTopicInfo ValidateForm()
        {
            LogTopicInfo info = new LogTopicInfo();
            info.Title = RequestTools.GetFormString("ttitle").Trim();
            info.Body = RequestTools.GetFormString("logbody").Trim();
            info.Tags = RequestTools.GetFormString("tags").Trim();
            if (info.Title.Length == 0)
            {
                this.AddErrLine("标题不能为空！");
                return null;

            }
            if (info.Title.Length > Config.TitleMaxSize)
            {
                this.AddErrLine("标题长度不能超过" + Config.TitleMaxSize);
                return null;
            }

            if (info.Body.Length == 0)
            {
                this.AddErrLine("内容不能为空!");
                return null;
            }
            else if (info.Body.Length > Config.TopicMaxSize)
            {
                this.AddErrLine("内容长度不能超过 " + Config.TopicMaxSize + "字符！");
                return null;
            }
            info.HostUserId = UserData.Id;
            info.PostUserName = UserData.UserName;
            info.PostTime = DateTime.Now;
            info.UpdateTime = DateTime.Now;
            return info;
        }
    }
}